package com.banyuan.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    private static String driverClassName;
    private static String url;
    private static String username;
    private static String password;
    private static DataSource dataSource;


    static {
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();

            try {
                properties.load(is);
                dataSource = DruidDataSourceFactory.createDataSource(properties);

            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception exception) {
                exception.printStackTrace();
            }


    }

    public static Connection getConnection() throws SQLException {
        Connection conn = dataSource.getConnection();
        return conn;
    }


    public static ResultSet executeQuery(String sql,Object ...objs) throws SQLException {
        Connection conn = getConnection();
        ResultSet rs = executeQuery(conn, sql, objs);
        return rs;
    }


    public static ResultSet executeQuery(Connection connection,String sql,Object ...objs) throws SQLException {
        PreparedStatement pstm = null;
        pstm = connection.prepareStatement(sql);
        for (int i = 0; i < objs.length; i++) {
            pstm.setObject(i+1,objs[i]);
        }
        ResultSet rs = pstm.executeQuery();
        return rs;

    }


    public static int executeUpdate(String sql,Object ...obj) throws SQLException {
        Connection conn = getConnection();
        try {
            return executeUpdate(conn, sql, obj);
        } finally {
            close(conn); //这是我给的conn，所以要关
        }

    }




    public static int executeUpdate(Connection conn,String sql,Object ...obj) throws SQLException {
        PreparedStatement pstm = null;
        try {
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                pstm.setObject(i+1,obj[i]);
            }

            return pstm.executeUpdate();
        }  finally {
            close(pstm);
        }
    }






    public static void closeBy(ResultSet rs) {
        try {
            if (rs != null) {
                Statement statement = rs.getStatement();
                Connection conn = statement.getConnection();
                close(rs);
                close(statement);
                close(conn);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }



    public static void close(AutoCloseable ac) {
        if (ac != null) {
            try {
                ac.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Connection connection = getConnection();
            System.out.println(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }




}
